/**
 * 文 件 名：BookController.java
 * CopyRight (c) 2013 Anze, Inc. All rights reserved.
 * 创 建 人：lurf
 * 日      期：2013-9-16 下午11:16:44
 * 描      述：
 */
package com.anze.abd.ui.book;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.anze.abd.persistence.domain.Book;
import com.anze.abd.service.book.BookService;
import com.anze.base.constant.BaseConstant;
import com.anze.base.persistence.domain.User;
import com.anze.base.ui.commons.BaseController;
import com.anze.base.utils.BeanUtils;
import com.anze.base.utils.PageNavInfoUtils;

/**
 * 描述：图书管理
 * @author lurf 2013-9-16
 * @version 2.0.0
 * @since 2.0.0
 */
@Controller
@RequestMapping(value="book")
public class BookController extends BaseController {
	
	@Resource(name = "bookService")
	private BookService bookService;

	/**
	 * 描述：列表
	 * @return
	 * @author lurf 2013-9-16
	 * @version 2.0.0
	 * @since 2.0.0
	 */
	@RequestMapping()
	public ModelAndView list() {
		PageNavInfoUtils.setPageNavInfo();
		return new ModelAndView("/book/book_list");
	}
	
	/**
	 * 描述：保存新增
	 * @param request
	 * @return
	 * @author lurf 2013-9-22
	 * @version 2.0.0
	 * @since 2.0.0
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public @ResponseBody String save(HttpServletRequest request) {
		Book book = formConverter.convertToBean(request, Book.class);
		Date curDate = new Date();
		User user = (User) request.getSession().getAttribute(BaseConstant.SESSION_USER);
		book.setCreator(user.getName());
		book.setUpdator(user.getName());
		book.setCreateTimestamp(curDate);
		book.setUpdateTimestamp(curDate);
		bookService.save(book);
		return "true";
	}

	/**
	 * 描述：保存更新
	 * @param request
	 * @param id
	 * @return
	 * @author lurf 2013-9-22
	 * @version 2.0.0
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @since 2.0.0
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public @ResponseBody String update(HttpServletRequest request, @PathVariable Long id) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		Book book1 = formConverter.convertToBean(request, Book.class);
		Book book = bookService.get(id);
		BeanUtils.copyNotNullProperties(book, book1, new String[]{});
		Date curDate = new Date();
		User user = (User) request.getSession().getAttribute(BaseConstant.SESSION_USER);
		book.setUpdator(user.getName());
		book.setUpdateTimestamp(curDate);
		bookService.update(book);
		return "true";
	}

	/**
	 * 描述：保存删除
	 * @param request
	 * @param id
	 * @return
	 * @author lurf 2013-9-22
	 * @version 2.0.0
	 * @since 2.0.0
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public @ResponseBody String del(HttpServletRequest request, @PathVariable Long id) {
		bookService.delete(id);
		return "true";
	}
	
}
